var CusBase64 = require('../../utils/base64.js');

Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function (e) {
    console.log(getApp().globalData)
    // 查看是否授权
    wx.showLoading({
      title: '加载中',
    })
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              wx.hideLoading()
              wx.login({
                success: res => {
                  wx.request({
                    url: getApp().data.service + '/menu/login',
                    data: {
                      code: res.code,
                      nickName: CusBase64.CusBASE64.encoder(getApp().globalData.userInfo.nickName)
                    },
                    header: {
                      'content-type': 'application/json'
                    },
                    success: function (res) {
                      console.log(res)
                      getApp().globalData.userid = res.data.data.id
                      console.log(e.id)
                      getApp().globalData.orderId = e.id
                      getApp().globalData.realName = CusBase64.CusBASE64.decoder(res.data.data.realName)
                      wx.switchTab({
                        url: '../index/index',
                      })
                    }
                  })
                }
              })
            }
          })
        }
        else{
          wx.hideLoading()
          wx.showToast({
            title: '尚未授权请点击按钮授权登录',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      getApp().globalData.userInfo = e.detail.userInfo
      wx.login({
        success: res => {
          wx.request({
            url: getApp().data.service + '/menu/login',
            data: {
              code: res.code,
              nickName: getApp().globalData.userInfo.nickName
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res)
              getApp().globalData.userid = res.data.data.id
              console.log(e.id)
              getApp().globalData.orderId = e.id
              getApp().globalData.realName = res.data.data.realName
              wx.switchTab({
                url: '../index/index',
              })
            }
          })
        }
      })
    } else {
      //用户按了拒绝按钮
    }
  }
})